package com.xingyun.kingdee.erp.common.model.response;


import io.swagger.annotations.ApiModelProperty;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lingo
 * @date 2020-08-11 17:35
 */

public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 成功代码
     */
    private static final String SUCCESS_CODE = HttpStatus.OK.value()+"";

    @ApiModelProperty("是否成功")
    private boolean success;
    @ApiModelProperty("编码")
    private String code;
    @ApiModelProperty("消息")
    private String msg;
    @ApiModelProperty("数据")
    private T data;
    @ApiModelProperty("额外数据")
    private Map<String, Object> extra;

    public Result() {
    }

    public Result<T> addExtraIfTrue(boolean bool, String key, Object value) {
        if (bool) {
            this.addExtra(key, value);
        }

        return this;
    }

    public Result<T> addExtra(String key, Object value) {
        this.extra = (Map)(this.extra == null ? new HashMap(16) : this.extra);
        this.extra.put(key, value);
        return this;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public Result<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getCode() {
        return this.code;
    }

    public Result<T> setCode(String code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return this.msg;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Map<String, Object> getExtra() {
        return this.extra;
    }

    public Result<T> setExtra(Map<String, Object> extra) {
        this.extra = extra;
        return this;
    }

    public static <T> Result<T> success() {
        return success(null);
    }


    public static Result failure(String code, String msg) {
        return (new Result()).setSuccess(false).setCode(code).setMsg(msg);
    }

    @SuppressWarnings("unchecked")
    public static <T> Result<T> success(T obj) {
        return (new Result()).setData(obj).setCode(SUCCESS_CODE).setMsg("OK").setSuccess(true);
    }
}
